﻿(function ($) {
    $.fn.musicPlayer = function (playlist, userOptions) {

        var deafaultOptions = { autoPlay: true,
            erroMessage: "This player does not support the functionality requested!",
            albumCover: "../../Content/Icons/cover.png",
            albumName: "unknown",
            artistName: "unknown"
        };

        var options = $.extend(deafaultOptions, userOptions);
        var currentSongId = null;
        var totalNumberOfSongs = 0;
        var repeat = false;
        var muted = false;
        var shuffle = false;

        function updateMuteButton(elem){
            var labelOption;
            var label = $(elem).button( "option", "label" );
            if(label === "mute-off") {
                muted = false;
                labelOption = {
                    label: "mute-on",
                    icons: {
                        primary: "ui-icon-volume-on"
                    }
                };
            }
            else{
                muted = true;
                labelOption = {
                    label: "mute-off",
                    icons: {
                        primary: "ui-icon-volume-off"
                    }
                };         
            }
            $(elem).button("option", labelOption);
            if (currentSongId != null) {
                $("#" + options.albumName + "-audio-" + currentSongId).get(0).muted = muted;
            }
        }
        function updatePlayButton(elem) {
            var labelOption;
            if ($(elem).text() === "play") {
                labelOption = {
                    label: "pause",
                    icons: {
                        primary: "ui-icon-pause"
                    }
                };
            }
            else {
                labelOption = {
                    label: "play",
                    icons: {
                        primary: "ui-icon-play"
                    }
                };
            }
            $(elem).button("option", labelOption);
        }
        function setPlayStatus(elem) {
            var labelOption = {
                label: "play",
                icons: {
                    primary: "ui-icon-play"
                }
            };
            $(elem).button("option", labelOption);
        }
        function setPauseStatus(elem) {
            var labelOption = {
                label: "pause",
                icons: {
                    primary: "ui-icon-pause"
                }
            };
            $(elem).button("option", labelOption);
        }

        return this.each(function () {

            var playerElem = document.createElement("div");
            $(playerElem).attr("class", "player").css("width", "300px").css("padding", "20px").css("background-color", "rgba(144, 144, 144, 0.3)");


            var playerMenu = document.createElement("div");
            $(playerMenu).attr("class", "player-menu");

//            var playerMenuCover = document.createElement("div");
//            $(playerMenuCover).attr("class", "player-menu-cover").css("float", "left");
//            $(playerMenu).append(playerMenuCover);

//            var playerCoverImage = document.createElement("img");
//            $(playerCoverImage).attr("src", options.albumCover).css("width", "140px").css("height", "140px");
//            $(playerCoverImage).attr("alt", "Album Cover");
//            $(playerMenuCover).append(playerCoverImage);

            var playerMenuControls = document.createElement("div");
            $(playerMenuControls).attr("class", "player-menu-controls").css("float", "left").css("max-width","245px");

            var playerMenuVolume = document.createElement("div");
            $(playerMenuVolume).attr("class", "player-menu-volume");
            $(playerMenuVolume).attr("id", "player-menu-volume" + options.albumName);

            var playerMenuControlsInfo = document.createElement("span");
            $(playerMenuControlsInfo).attr("class", "player-menu-controls-info").css("float", "left").css("margin-left", "10px");

            var songName = document.createElement("span");
            $(songName).attr("class", "player-menu-controls-info-songname");
            $(songName).attr("id", "title-" + options.albumName).css("display", "block").css("font-size", "15px");


            var artistName = document.createElement("span");
            $(artistName).attr("class", "player-menu-controls-info-artistname");
            $(artistName).append(options.albumName + " - " + options.artistName ).css("display", "block");        

            $(playerMenuControlsInfo).append(songName);
            $(playerMenuControlsInfo).append(artistName);

            $(playerMenuControls).append(playerMenuControlsInfo);
            
            var songProgress = document.createElement("span");
            $(songProgress).attr("class", "player-menu-controls-info-songProgress").css("display", "block").css("margin-top", "70px").css("margin-left", "15px");

            var songProgressRange = document.createElement("input");
            $(songProgressRange).attr("type", "range");
            $(songProgressRange).attr("step", "any");
            $(songProgressRange).attr("value", 0);
            $(songProgressRange).attr("class","player-menu-controls-seekbar");
            $(songProgressRange).attr("id", "seekbar-" + options.albumName);

            $(songProgress).append(songProgressRange);

            var songProgressLabel = document.createElement("label");
            $(songProgressLabel).attr("id", "label-" + options.albumName).css("margin-left", "5px");
            $(songProgressLabel).append("-:--:--");

            $(songProgress).append(songProgressLabel)
            $(playerMenuControls).append(songProgress);
            
            var playerMenuControlsTop = document.createElement("span");
            $(playerMenuControlsTop).attr("class", "ui-widget-header ui-corner-all player-menu-controls-top").css("display","block").css("margin-left", "3px").css("padding", "5px 4px");

            var buttonPrev = document.createElement("button");
            $(buttonPrev).attr("id", "beginning-" + options.albumName);
            $(buttonPrev).append("previous");

            var buttonPlay = document.createElement("button");
            $(buttonPlay).attr("id", "play-" + options.albumName);
            $(buttonPlay).append("play");

            var buttonStop = document.createElement("button");
            $(buttonStop).attr("id", "stop-" + options.albumName);
            $(buttonStop).append("stop");

            var buttonNext = document.createElement("button");
            $(buttonNext).attr("id", "end-" + options.albumName);
            $(buttonNext).append("next");

            $(playerMenuControlsTop).append(buttonPrev);
            $(playerMenuControlsTop).append(buttonPlay);
            $(playerMenuControlsTop).append(buttonStop);
            $(playerMenuControlsTop).append(buttonNext);

            $(playerMenuControls).append(playerMenuControlsTop);

            // var playerMenuControlsBottom = document.createElement("span");
            //$(playerMenuControlsBottom).attr("class", "ui-widget-header ui-corner-all player-menu-controls-top");

            var buttonShuffle = document.createElement("input");
            $(buttonShuffle).attr("type", "checkbox");
            $(buttonShuffle).attr("id", "shuffle-" + options.albumName);

            var buttonShuffleLabel = document.createElement("label");
            $(buttonShuffleLabel).attr("for", "shuffle-" + options.albumName);
            $(buttonShuffleLabel).append("shuffle");

            var buttonRepeat = document.createElement("input");
            $(buttonRepeat).attr("type", "checkbox");
            $(buttonRepeat).attr("id", "repeat-" + options.albumName);

            var buttonReapeatLabel = document.createElement("label");
            $(buttonReapeatLabel).attr("for", "repeat-" + options.albumName);
            $(buttonReapeatLabel).append("repeat");

            var buttonMute = document.createElement("input");
            $(buttonMute).attr("type", "checkbox");
            $(buttonMute).attr("id", "mute-" + options.albumName);

            var buttonMuteLabel = document.createElement("label");
            $(buttonMuteLabel).attr("for", "mute-" + options.albumName);
            $(buttonMuteLabel).append("mute-on");

            $(playerMenuControlsTop).append(buttonShuffle);
            $(playerMenuControlsTop).append(buttonShuffleLabel);
            $(playerMenuControlsTop).append(buttonRepeat);
            $(playerMenuControlsTop).append(buttonReapeatLabel);
            $(playerMenuControlsTop).append(buttonMute);
            $(playerMenuControlsTop).append(buttonMuteLabel);

            // $(playerMenuControls).append(playerMenuControlsBottom);

            $(playerMenu).append(playerMenuControls);
            $(playerMenu).append(playerMenuVolume);

            $(playerElem).append(playerMenu);

            var playerClear = document.createElement("div");
            $(playerClear).attr("class", "clear");

            $(playerElem).append(playerClear);

            var playerSongs = document.createElement("div");
            $(playerSongs).attr("class", "player-songs");

            var playerSongsList = document.createElement("ul");
            $(playerSongsList).attr("class", "player-songs-list").css("list-style-type", "none");

            var hidden = document.createElement("div");
            totalNumberOfSongs = playlist.length - 1;
            for (var i = 0; i < playlist.length; i++) {
                var audioElem;
                var songListItem = document.createElement("li");
                $(songListItem).attr("class", "player-songs-list-item-" + options.albumName + i);
                $(songListItem).css("padding-left", "5px").css("padding-right","5px");

                var songListItemLink = document.createElement("a");
                $(songListItemLink).attr("href", "#");
                $(songListItemLink).attr("class", "player-songs-list-item-song song-item-" + options.albumName);
                $(songListItemLink).attr("id", options.albumName + i);
                $(songListItemLink).append(playlist[i].name);

                var songLength = document.createElement("span");
                $(songLength).attr("class", "player-songs-list-item-song-length");
                $(songLength).css("float", "right");
                $(songLength).append(playlist[i].length);
                $(songListItemLink).append(songLength);
                

                if (Modernizr.audio) {
                    //alert("audio is supported");
                    audioElem = document.createElement("audio");
                    $(audioElem).attr("id", options.albumName + "-audio-" + i);
                    $(audioElem).attr("hidden", "hidden");
                    $(audioElem).attr("controls", "controls");
                    if (i == 0) {
                        $(audioElem).attr("autoplay", options.autoPlay);
                        if (options.autoPlay == true) {
                            currentSongId = 0;
                        }
                    }

                    var srcElem = document.createElement("source");
                    $(srcElem).attr("src", playlist[i].src);
                    $(srcElem).attr("type", "audio/mp3");
                    $(audioElem).append(srcElem);

                    $(songListItem).append(audioElem);
                }
                else {
                    //alert("audio is not supported");              
                    audioElem = document.createElement("object");
                    $(audioElem).attr("id", options.albumName + "-audio-" + i);
                    $(audioElem).css("visibility", "hidden");
                    $(audioElem).attr("classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000");
                    $(audioElem).attr("codebase", "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0");
                    $(audioElem).attr("align", "");
                    $(audioElem).attr("width", "0");
                    $(audioElem).attr("heigth", "0");

                    param1 = document.createElement("param");
                    $(param1).attr("name","movie");
                    $(param1).attr("value","../../Scripts/mp3Flash/niftyplayer.swf?file="+ playlist[i].src + "&as=0");

                    param2 = document.createElement("param");
                    $(param2).attr("name","quality");
                    $(param2).attr("value","high");

                    param3 = document.createElement("param");
                    $(param3).attr("name","bgcolor");
                    $(param3).attr("value","#FFFFFF");

                    embed = document.createElement("embed");
                    $(embed).attr("src","../../Scripts/mp3Flash/niftyplayer.swf?file=" + playlist[i].src + "&as=0");
                    $(embed).attr("quality","high");
                    $(embed).attr("bgcolor","#FFFFFF");
                    $(embed).attr("name", options.albumName + "-audio-" + i);
                    $(embed).attr("align","");
                    $(embed).attr("type","application/x-shockwave-flash");
                    $(embed).attr("pluginspage","http://www.macromedia.com/go/getflashplayer");
                    $(embed).attr("width", "0");
                    $(embed).attr("heigth", "0");
                    
                    $(audioElem).append(param1);
                    $(audioElem).append(param2);
                    $(audioElem).append(param3);
                    $(audioElem).append(embed);

                    $(hidden).append(audioElem);
                }
                
                $(songListItem).append(songListItemLink);
                $(playerSongsList).append(songListItem);
            }

            $(playerSongs).append(playerSongsList);

            $(playerElem).append(playerSongs);

            $(this).hide().append(playerElem).fadeIn("slow");
            $(this).append(hidden);

            $("#player-menu-volume" + options.albumName).slider({
                orientation: "vertical",
                range: "min",
                min: 0,
                max: 100,
                value: 50,
                //slide: function (event, ui) {
                    //alert(ui.value);
               // }
            }).css("float", "left").css("margin-left", "20px").css("height", "140px");

            $("#beginning-" + options.albumName).button({
                text: false,
                icons: {
                    primary: "ui-icon-seek-start"
                }
            });
            $("#rewind-" + options.albumName).button({
                text: false,
                icons: {
                    primary: "ui-icon-seek-prev"
                }
            });
            $("#play-" + options.albumName).button({
                name: options.albumName,
                text: false,
                icons: {
                    primary: "ui-icon-play"
                }
            })
		            .click(function () {
		                updatePlayButton(this);
		            });
            $("#stop-" + options.albumName).button({
                text: false,
                icons: {
                    primary: "ui-icon-stop"
                }
            })
		            .click(function () {
		                $("#play").button("option", {
		                    label: "play",
		                    icons: {
		                        primary: "ui-icon-play"
		                    }
		                });
		            });
            $("#forward-" + options.albumName).button({
                text: false,
                icons: {
                    primary: "ui-icon-seek-next"
                }
            });
            $("#end-" + options.albumName).button({
                text: false,
                icons: {
                    primary: "ui-icon-seek-end"
                }
            });
            $("#shuffle-" + options.albumName).button({
                text : false,
                icons: {
                    primary: "ui-icon-shuffle"
                }            
            })
            .click(function() {
                if(shuffle == false)
                {
                    shuffle = true;
                }
                else
                {
                    shuffle = false;
                }              
            });
            $("#repeat-" + options.albumName).button({
                text : false,
                icons: {
                    primary: "ui-icon-refresh"
                }
            })
            .click(function() {
                if(repeat == false)
                {
                    repeat = true;
                }
                else
                {
                    repeat = false;
                }
                if (currentSongId != null) {
                    $("#" + options.albumName + "-audio-" + currentSongId).get(0).loop = repeat;
                }                
            });
            $("#mute-" + options.albumName).button({
                text : false,
                icons: {
                    primary: "ui-icon-volume-on"
                }
            })
		    .click(function () {
		        updateMuteButton(this);
		    });
            //Disable mute and repeat botton for nifty here!!!
            if(Modernizr.audio) {
            }
            else {
                $("#mute-" + + options.albumName).button( "option", "disabled", true );
                $("#repeat-" + + options.albumName).button( "option", "disabled", true );
                $("#player-menu-volume" + options.albumName).slider( "option", "disabled", true );
                $(".player-menu-controls-info-songProgress").css("visibility", "hidden");
            }

            $("#stop-" + options.albumName).click(function () {
                if (currentSongId != null) {
                    setPlayStatus($("#play"));
                    if(Modernizr.audio) {
                        $("#" + options.albumName + "-audio-" + currentSongId).get(0).pause();
                        $("#" + options.albumName + "-audio-" + currentSongId).get(0).currentTime = 0;
                    }
                    else {
                        niftyplayer(options.albumName + "-audio-" + currentSongId).stop();
                    }
                    $("#" + options.albumName + currentSongId).hide().css("color", "rgb(40,141,177)").fadeIn("slow");
                    $(".player-songs-list-item-" + options.albumName + currentSongId).hide().css("background-color", "rgba(50,50,50,0.6)").fadeIn("slow");
                }
            });
            $("#play-" + options.albumName).click(function () {
                if (currentSongId == null) {
                    if(shuffle) { 
                        currentSongId = Math.floor(Math.random()*playlist.length);
                    }
                    else {
                        currentSongId = 0;
                    }
                    $("#title-" + options.albumName).hide().append(playlist[currentSongId].name).fadeIn("slow");
                    if(Modernizr.audio) {
                        $("#" + options.albumName + "-audio-" + currentSongId).get(0).muted = muted;
                        $("#" + options.albumName + "-audio-" + currentSongId).get(0).loop = repeat;
                        $("#" + options.albumName + "-audio-" + currentSongId).get(0).play();                        
                    }
                    else {                     
                        niftyplayer(options.albumName + "-audio-" + currentSongId).play();                        
                    }
                    $("#" + options.albumName + currentSongId).hide().css("color", "rgb(40,141,177)").fadeIn("slow");
                    $(".player-songs-list-item-" + options.albumName + currentSongId).hide().css("background-color", "rgba(50,50,50,0.6)").fadeIn("slow");
                }
                else {
                    if(Modernizr.audio) {
                        if ($("#" + options.albumName + "-audio-" + currentSongId).get(0).paused == false) {
                            $("#" + options.albumName + "-audio-" + currentSongId).get(0).pause();
                            $("#" + options.albumName + currentSongId).hide().css("color", "rgb(40,141,177)").fadeIn("slow");
                            $(".player-songs-list-item-" + options.albumName + currentSongId).hide().css("background-color", "rgba(50,50,50,0.6)").fadeIn("slow");
                        }
                        else {
                            $("#title-" + options.albumName).fadeOut("slow").empty().append(playlist[currentSongId].name).fadeIn("slow");
                            $("#" + options.albumName + "-audio-" + currentSongId).get(0).muted = muted;
                            $("#" + options.albumName + "-audio-" + currentSongId).get(0).loop = repeat;
                            $("#" + options.albumName + "-audio-" + currentSongId).get(0).play();
                            $("#" + options.albumName + currentSongId).hide().css("color", "rgb(40,141,177)").fadeIn("slow");
                            $(".player-songs-list-item-" + options.albumName + currentSongId).hide().css("background-color", "rgba(50,50,50,0.6)").fadeIn("slow");
                        }
                    }
                    else {
                        if (niftyplayer(options.albumName + "-audio-" + currentSongId).getState() == "playing") {
                            niftyplayer(options.albumName + "-audio-" + currentSongId).pause();
                            $("#" + options.albumName + currentSongId).hide().css("color", "rgb(40,141,177)").fadeIn("slow");
                            $(".player-songs-list-item-" + options.albumName + currentSongId).hide().css("background-color", "rgba(50,50,50,0.6)").fadeIn("slow");
                        }
                        else {
                            $("#title-" + options.albumName).fadeOut("slow").empty().append(playlist[currentSongId].name).fadeIn("slow");
                            //$("#" + options.albumName + "-audio-" + currentSongId).get(0).muted = muted;
                            //$("#" + options.albumName + "-audio-" + currentSongId).get(0).loop = repeat;
                            niftyplayer(options.albumName + "-audio-" + currentSongId).play();
                            $("#" + options.albumName + currentSongId).hide().css("color", "rgb(40,141,177)").fadeIn("slow");
                            $(".player-songs-list-item-" + options.albumName + currentSongId).hide().css("background-color", "rgba(50,50,50,0.6)").fadeIn("slow");
                        }                    
                    }
                }
                $("#" + options.albumName + "-audio-" + currentSongId).get(0).addEventListener("timeupdate", UpdateTheTime, false);
                $("#" + options.albumName + "-audio-" + currentSongId).get(0).addEventListener("durationchange", SetSeekBar, false);
                $("#" + options.albumName + "-audio-" + currentSongId).get(0).addEventListener("ended", PlayNextSong, false);              
            });
            $("#end-" + options.albumName).click(function () {
                if (currentSongId == null) {
                    if(shuffle) { 
                        currentSongId = Math.floor(Math.random()*playlist.length);
                    }
                    else {
                        currentSongId = 0;
                    }
                    $("#title-" + options.albumName).hide().append(playlist[currentSongId].name).fadeIn("slow");
                    if(Modernizr.audio) {
                        $("#" + options.albumName + "-audio-" + currentSongId).get(0).muted = muted;
                        $("#" + options.albumName + "-audio-" + currentSongId).get(0).loop = repeat;
                        $("#" + options.albumName + "-audio-" + currentSongId).get(0).play();
                    }
                    else {
                        //$("#" + options.albumName + "-audio-" + currentSongId).get(0).muted = muted;
                        //$("#" + options.albumName + "-audio-" + currentSongId).get(0).loop = repeat;
                        niftyplayer(options.albumName + "-audio-" + currentSongId).play();
                    }
                    $("#" + options.albumName + currentSongId).hide().css("color", "rgb(40,141,177)");
                    $(".player-songs-list-item-" + options.albumName + currentSongId).hide().css("background-color", "rgba(50,50,50,0.6)").fadeIn("slow");
                }
                else {
                    $("#" + options.albumName + currentSongId).hide().css("color", "white").fadeIn("slow");
                    $(".player-songs-list-item-" + options.albumName + currentSongId).hide().css("background-color", "").fadeIn("slow");
                    if(Modernizr.audio) {
                        $("#" + options.albumName + "-audio-" + currentSongId).get(0).pause();
                        $("#" + options.albumName + "-audio-" + currentSongId).get(0).currentTime = 0;
                    }
                    else {                        
                        niftyplayer(options.albumName + "-audio-" + currentSongId).stop();
                    }
                    if(shuffle) { 
                        currentSongId = Math.floor(Math.random()*playlist.length);
                    }
                    else {
                        if (currentSongId == totalNumberOfSongs) {
                            currentSongId = 0;
                        }
                        else {
                            currentSongId = parseInt(currentSongId) + 1;
                        }
                    }
                    $("#title-" + options.albumName).fadeOut("slow").empty().append(playlist[currentSongId].name).fadeIn("slow");
                    setPauseStatus($("#play"));
                    if(Modernizr.audio) {
                        $("#" + options.albumName + "-audio-" + currentSongId).get(0).muted = muted;
                        $("#" + options.albumName + "-audio-" + currentSongId).get(0).loop = repeat;
                        $("#" + options.albumName + "-audio-" + currentSongId).get(0).play();
                    }
                    else {
                        //$("#" + options.albumName + "-audio-" + currentSongId).get(0).muted = muted;
                        //$("#" + options.albumName + "-audio-" + currentSongId).get(0).loop = repeat;
                        niftyplayer(options.albumName + "-audio-" + currentSongId).play();
                    }
                    $("#" + options.albumName + currentSongId).hide().css("color", "rgb(40,141,177)").fadeIn("slow");
                    $(".player-songs-list-item-" + options.albumName + currentSongId).hide().css("background-color", "rgba(50,50,50,0.6)").fadeIn("slow");
                }
                $("#" + options.albumName + "-audio-" + currentSongId).get(0).addEventListener("timeupdate", UpdateTheTime, false);
                $("#" + options.albumName + "-audio-" + currentSongId).get(0).addEventListener("durationchange", SetSeekBar, false);
                $("#" + options.albumName + "-audio-" + currentSongId).get(0).addEventListener("ended", PlayNextSong, false);
            });
            $("#beginning-" + options.albumName).click(function () {
                if (currentSongId == null) {
                    if(shuffle) { 
                        currentSongId = Math.floor(Math.random()*playlist.length);
                    }
                    else {
                        currentSongId = 0;
                    }
                    $("#title-" + options.albumName).hide().append(playlist[currentSongId].name).fadeIn("slow");
                    if(Modernizr.audio) {
                        $("#" + options.albumName + "-audio-" + currentSongId).get(0).muted = muted;
                        $("#" + options.albumName + "-audio-" + currentSongId).get(0).loop = repeat;
                        $("#" + options.albumName + "-audio-" + currentSongId).get(0).play();
                    }
                    else {
                        //$("#" + options.albumName + "-audio-" + currentSongId).get(0).muted = muted;
                        //$("#" + options.albumName + "-audio-" + currentSongId).get(0).loop = repeat;
                        niftyplayer(options.albumName + "-audio-" + currentSongId).play();
                    }
                    $("#" + options.albumName + currentSongId).hide().css("color", "rgb(40,141,177)").fadeIn("slow");
                    $(".player-songs-list-item-" + options.albumName + currentSongId).hide().css("background-color", "rgba(50,50,50,0.6)").fadeIn("slow");
                }
                else {
                    $("#" + options.albumName + currentSongId).hide().css("color", "white").fadeIn("slow");
                    $(".player-songs-list-item-" + options.albumName + currentSongId).hide().css("background-color", "").fadeIn("slow");
                    if(Modernizr.audio) {                    
                        $("#" + options.albumName + "-audio-" + currentSongId).get(0).pause();
                        $("#" + options.albumName + "-audio-" + currentSongId).get(0).currentTime = 0;
                    }
                    else {
                        niftyplayer(options.albumName + "-audio-" + currentSongId).stop();
                    }
                    if(shuffle) { 
                        currentSongId = Math.floor(Math.random()*playlist.length);
                    }
                    else if (currentSongId != 0) {

                        currentSongId = currentSongId - 1;
                    }
                    $("#title-" + options.albumName).fadeOut("slow").empty().append(playlist[currentSongId].name).fadeIn("slow");
                    setPauseStatus($("#play"));
                    if(Modernizr.audio) {
                        $("#" + options.albumName + "-audio-" + currentSongId).get(0).muted = muted;
                        $("#" + options.albumName + "-audio-" + currentSongId).get(0).loop = repeat;
                        $("#" + options.albumName + "-audio-" + currentSongId).get(0).play();
                    }
                    else {
                        //$("#" + options.albumName + "-audio-" + currentSongId).get(0).muted = muted;
                        //$("#" + options.albumName + "-audio-" + currentSongId).get(0).loop = repeat;
                        niftyplayer(options.albumName + "-audio-" + currentSongId).play();
                    }
                    $("#" + options.albumName + currentSongId).hide().css("color", "rgb(40,141,177)").fadeIn("slow");
                    $(".player-songs-list-item-" + options.albumName + currentSongId).hide().css("background-color", "rgba(50,50,50,0.6)").fadeIn("slow");
                }
                $("#" + options.albumName + "-audio-" + currentSongId).get(0).addEventListener("timeupdate", UpdateTheTime, false);
                $("#" + options.albumName + "-audio-" + currentSongId).get(0).addEventListener("durationchange", SetSeekBar, false);
                $("#" + options.albumName + "-audio-" + currentSongId).get(0).addEventListener("ended", PlayNextSong, false);
            });
            $(".song-item-" + options.albumName).click(function () {
                var songId = $(this).attr("id");
                var id = songId.match(/\d+$/);
                var elem = $("#play");
                if (currentSongId != null) {
                    if (currentSongId.toString() === id.toString()) {
                        if(Modernizr.audio) {
                            if ($("#" + options.albumName + "-audio-" + currentSongId).get(0).paused == false) {
                                $("#" + options.albumName + "-audio-" + currentSongId).get(0).pause();
                                $("#" + options.albumName + currentSongId).hide().css("color", "white").fadeIn("slow");
                                $(".player-songs-list-item-" + options.albumName + currentSongId).hide().css("background-color", "").fadeIn("slow");
                                setPlayStatus(elem);
                            }
                            else {
                                $("#" + options.albumName + "-audio-" + currentSongId).get(0).muted = muted;
                                $("#" + options.albumName + "-audio-" + currentSongId).get(0).loop = repeat;
                                $("#" + options.albumName + "-audio-" + currentSongId).get(0).play();
                                $("#" + options.albumName + currentSongId).hide().css("color", "rgb(40,141,177)").fadeIn("slow");
                                $(".player-songs-list-item-" + options.albumName + currentSongId).hide().css("background-color", "rgba(50,50,50,0.6)").fadeIn("slow");
                                setPauseStatus(elem);
                            }
                        }
                        else {
                            if (niftyplayer(options.albumName + "-audio-" + currentSongId).getState() == "playing") {
                                niftyplayer(options.albumName + "-audio-" + currentSongId).pause();
                                $("#" + options.albumName + currentSongId).hide().css("color", "white").fadeIn("slow");
                                $(".player-songs-list-item-" + options.albumName + currentSongId).hide().css("background-color", "").fadeIn("slow");
                                setPlayStatus(elem);
                            }
                            else {
                                //$("#" + options.albumName + "-audio-" + currentSongId).get(0).muted = muted;
                                //$("#" + options.albumName + "-audio-" + currentSongId).get(0).loop = repeat;
                                niftyplayer(options.albumName + "-audio-" + currentSongId).play();
                                $("#" + options.albumName + currentSongId).hide().css("color", "rgb(40,141,177)").fadeIn("slow");
                                $(".player-songs-list-item-" + options.albumName + currentSongId).hide().css("background-color", "rgba(50,50,50,0.6)").fadeIn("slow");
                                setPauseStatus(elem);
                            }                        
                        }
                    }
                    else {
                        if(Modernizr.audio) {
                            $("#" + options.albumName + "-audio-" + currentSongId).get(0).pause();
                            $("#" + options.albumName + "-audio-" + currentSongId).get(0).currentTime = 0;
                        }
                        else {
                            niftyplayer(options.albumName + "-audio-" + currentSongId).stop();
                        }
                        $("#" + options.albumName + currentSongId).hide().css("color", "white").fadeIn("slow");
                        $(".player-songs-list-item-" + options.albumName + currentSongId).hide().css("background-color", "").fadeIn("slow");
                        currentSongId = id;
                        $("#title-" + options.albumName).fadeOut("slow").empty().append(playlist[currentSongId].name).fadeIn("slow");
                        if(Modernizr.audio) {
                            $("#" + options.albumName + "-audio-" + currentSongId).get(0).muted = muted;
                            $("#" + options.albumName + "-audio-" + currentSongId).get(0).loop = repeat;
                            $("#" + options.albumName + "-audio-" + id).get(0).play();
                        }
                        else {
                            //$("#" + options.albumName + "-audio-" + currentSongId).get(0).muted = muted;
                            //$("#" + options.albumName + "-audio-" + currentSongId).get(0).loop = repeat;
                            niftyplayer(options.albumName + "-audio-" + currentSongId).play();
                        }
                        $("#" + options.albumName + currentSongId).hide().css("color", "rgb(40,141,177)").fadeIn("slow");
                        $(".player-songs-list-item-" + options.albumName + currentSongId).hide().css("background-color", "rgba(50,50,50,0.6)").fadeIn("slow");
                        setPauseStatus(elem);
                    }
                }
                else {
                    currentSongId = id;
                    $("#title-" + options.albumName).hide().append(playlist[currentSongId].name).fadeIn("slow");
                    if(Modernizr.audio) {
                        $("#" + options.albumName + "-audio-" + currentSongId).get(0).muted = muted;
                        $("#" + options.albumName + "-audio-" + currentSongId).get(0).loop = repeat;
                        $("#" + options.albumName + "-audio-" + id).get(0).play();
                    }
                    else {
                        //$("#" + options.albumName + "-audio-" + currentSongId).get(0).muted = muted;
                        //$("#" + options.albumName + "-audio-" + currentSongId).get(0).loop = repeat;                        
                        niftyplayer(options.albumName + "-audio-" + currentSongId).play();
                    }
                    $("#" + options.albumName + currentSongId).hide().css("color", "rgb(40,141,177)").fadeIn("slow");
                    $(".player-songs-list-item-" + options.albumName + currentSongId).hide().css("background-color", "rgba(50,50,50,0.6)").fadeIn("slow");
                    setPauseStatus(elem);                  
                }
                $("#" + options.albumName + "-audio-" + currentSongId).get(0).addEventListener("timeupdate", UpdateTheTime, false);
                $("#" + options.albumName + "-audio-" + currentSongId).get(0).addEventListener("durationchange", SetSeekBar, false);
                $("#" + options.albumName + "-audio-" + currentSongId).get(0).addEventListener("ended", PlayNextSong, false);
            });
            $("#player-menu-volume" + options.albumName).slider({
                change: function(event,ui){
                //TODO passa uppá að volume haldi sér fyrir næsta lag líka.
                    if(currentSongId != null){
                        $("#" + options.albumName + "-audio-" + currentSongId).get(0).volume = (ui.value / 100); 
                    }
                }   
            });
            $("#seekbar-" + options.albumName).change(function(){
                if(currentSongId != null){
                    var seekbar = document.getElementById("seekbar-" + options.albumName);
                    var audio = $("#" + options.albumName + "-audio-" + currentSongId).get(0);
                    seekbar.min = 0;
                    seekbar.max = audio.duration;                     
                    audio.currentTime = seekbar.value;
                }
            });
            function SetSeekBar() {
                var seekbar = document.getElementById("seekbar-" + options.albumName);
                var audio = $("#" + options.albumName + "-audio-" + currentSongId).get(0);
                seekbar.min = 0;
                seekbar.max = audio.duration;
            }
            function UpdateTheTime() {
                var seekbar = document.getElementById("seekbar-" + options.albumName);
                var audio = $("#" + options.albumName + "-audio-" + currentSongId).get(0);
                var sec = audio.currentTime;
                var h = Math.floor(sec/3600);
                sec = sec % 3600;
                var min = Math.floor(sec/60);
                sec = Math.floor(sec % 60);
                if(sec.toString().length < 2) sec = "0" + sec;
                if(min.toString().length < 2) min = "0" + min;
                document.getElementById("label-" + options.albumName).innerHTML = h + ":" + min + ":" + sec;

                seekbar.min = audio.startTime;
                seekbar.max = audio.duration;
                seekbar.value = audio.currentTime;
            }
            function PlayNextSong() {
                $("#end-" + options.albumName).click();              
            }
        });
    };
})(jQuery);